package com.ss.android.common.applog;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Debug;

import com.bytedance.common.b.WebViewPauseResumeCompat;
import com.bytedance.common.b.f;
import com.bytedance.common.utility.Logger;
import com.ss.android.common.app.ActivityStackManager;
import com.ss.android.common.util.ToolUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public final class i {
    private static Set a;
    private static Set b;

    static {
        i.a = new HashSet();
        i.b = new HashSet();
        i.a.add("ThreadPlus");
        i.a.add("ApiDispatcher");
        i.a.add("ApiLocalDispatcher");
        i.a.add("AsyncLoader");
        i.a.add("AsyncTask");
        i.a.add("Binder");
        i.a.add("PackageProcessor");
        i.a.add("SettingsObserver");
        i.a.add("WifiManager");
        i.a.add("JavaBridge");
        i.a.add("Compiler");
        i.a.add("Signal Catcher");
        i.a.add("GC");
        i.a.add("ReferenceQueueDaemon");
        i.a.add("FinalizerDaemon");
        i.a.add("FinalizerWatchdogDaemon");
        i.a.add("CookieSyncManager");
        i.a.add("RefQueueWorker");
        i.a.add("CleanupReference");
        i.a.add("VideoManager");
        i.a.add("DBHelper-AsyncOp");
        i.a.add("InstalledAppTracker2");
        i.a.add("AppData-AsyncOp");
        i.a.add("IdleConnectionMonitor");
        i.a.add("LogReaper");
        i.a.add("ActionReaper");
        i.a.add("Okio Watchdog");
        i.a.add("CheckWaitingQueue");
        i.b.add("com.facebook.imagepipeline.core.PriorityThreadFactory");
        i.b.add("com.ss.android.common.util.SimpleThreadFactory");
    }

    public static JSONObject a(Context arg10, Throwable arg11) {
        Object v0_2 = null;
        Context v2_2;
        Context v3;
        JSONObject v0 = null;
        if (arg11 != null) {
            JSONObject v1 = new JSONObject();
            if (arg10 != null) {
                v3 = arg10.getApplicationContext();
            } else {
                v3 = arg10;
            }

            try {
                StringWriter v2 = new StringWriter();
                PrintWriter v4 = new PrintWriter(((Writer) v2));
                arg11.printStackTrace(v4);
                Throwable v5 = arg11.getCause();
                if (v5 != null) {
                    v5.printStackTrace(v4);
                    v5 = v5.getCause();
                    if (v5 != null) {
                        v5.printStackTrace(v4);
                    }
                }

                String v2_1 = v2.toString();
                v4.close();
                if (v2_1 == null) {
                    return v1;
                }

                v1.put("data", v2_1);
                v1.put("crash_time", System.currentTimeMillis());
                v2_1 = "";
                if (v3 != null) {
                    v2_1 = ToolUtils.getCurProcessName(v3);
                    v1.put("process_name", v2_1);
                    if (!ToolUtils.isMainProcess(v3)) {
                        v1.put("remote_process", 1);
                    }
                }

                String v4_1 = v2_1;
                v1.put("app_count", AppLog.n);
                if (v3 == null) {

                } else {
                    if (v3 != null) {
                        v2_2 = v3.getApplicationContext();
                    } else {
                        v2_2 = v3;
                    }

                    Debug.MemoryInfo v5_1 = new Debug.MemoryInfo();
                    Debug.getMemoryInfo(v5_1);
                    JSONObject v6 = new JSONObject();
                    v6.put("dalvikPrivateDirty", v5_1.dalvikPrivateDirty);
                    v6.put("dalvikPss", v5_1.dalvikPss);
                    v6.put("dalvikSharedDirty", v5_1.dalvikSharedDirty);
                    v6.put("nativePrivateDirty", v5_1.nativePrivateDirty);
                    v6.put("nativePss", v5_1.nativePss);
                    v6.put("nativeSharedDirty", v5_1.nativeSharedDirty);
                    v6.put("otherPrivateDirty", v5_1.otherPrivateDirty);
                    v6.put("otherPss", v5_1.otherPss);
                    v6.put("otherSharedDirty", v5_1.otherSharedDirty);
                    v6.put("totalPrivateClean", m.a(v5_1));
                    v6.put("totalPrivateDirty", v5_1.getTotalPrivateDirty());
                    v6.put("totalPss", v5_1.getTotalPss());
                    v6.put("totalSharedClean", m.b(v5_1));
                    v6.put("totalSharedDirty", v5_1.getTotalSharedDirty());
                    v6.put("totalSwappablePss", m.c(v5_1));
                    v1.put("memory_info", v6);
                    if (v2_2 != null) {
                        JSONObject v5_2 = new JSONObject();
                        ActivityManager.MemoryInfo v6_1 = new ActivityManager.MemoryInfo();
                        v0_2 = v2_2.getSystemService(Context.ACTIVITY_SERVICE);
                        ((ActivityManager) v0_2).getMemoryInfo(v6_1);
                        v5_2.put("availMem", v6_1.availMem);
                        v5_2.put("lowMemory", v6_1.lowMemory);
                        v5_2.put("threshold", v6_1.threshold);
                        v5_2.put("totalMem", f.a(v6_1));
                        v1.put("sys_memory_info", v5_2);
                    }
                    try {
                        JSONObject v2_3 = new JSONObject();
                        v2_3.put("native_heap_size", Debug.getNativeHeapSize());
                        v2_3.put("native_heap_alloc_size", Debug.getNativeHeapAllocatedSize());
                        v2_3.put("native_heap_free_size", Debug.getNativeHeapFreeSize());
                        Runtime v5_3 = Runtime.getRuntime();
                        v2_3.put("max_memory", v5_3.maxMemory());
                        v2_3.put("free_memory", v5_3.freeMemory());
                        v2_3.put("total_memory", v5_3.totalMemory());
                        if (v0_2 != null) {
                            v2_3.put("memory_class", ((ActivityManager) v0_2).getMemoryClass());
                            v2_3.put("large_memory_class", WebViewPauseResumeCompat.getLargeMemoryClass(((ActivityManager) v0_2)));
                        }
                        v1.put("app_memory_info", v2_3);

                    } catch (Throwable v0_1) {
                        Logger.w("CrashUtil", "get memory info exception: " + v0_1);
                    }
                    label_149:
                    if (!i.a(arg11) && !i.a(v4_1, arg11)) {
                        return v1;
                    }

                    if (ToolUtils.isMainProcess(v3)) {
                        String v0_3 = ActivityStackManager.getFinishedActivitiesString();
                        if (Logger.debug()) {
                            Logger.d("OOM_Exception", "finishedActivities = " + v0_3 + " ExMsg = " + arg11.getMessage());
                        }
                        v1.put("finished_activities", v0_3);
                    } else {
                        if (v4_1 == null) {

                        } else {
                            if (v4_1.endsWith(":ad")) {
                                v1.put("data_files", i.a(v3));
                            }
                        }
                    }
                }

                v1.put("all_thread_stacks", i.a());
            } catch (Throwable v0_1) {
                Logger.w("CrashUtil", "handle crash exception: " + v0_1);
            }
            v0 = v1;
        }

        return v0;
    }

    private static String a() {
        String v0_4;
        int v4_1;
        JSONArray v10_1;
        int v2;
        Object v1;
        JSONObject v9;
        Object v0_2;
        JSONArray v7;
        JSONObject v6;
        try {
            Map<Thread, StackTraceElement[]> v0_1 = Thread.getAllStackTraces();
            v6 = new JSONObject();
            if (v0_1 != null) {
                v6.put("tr_all_count", v0_1.size());
            }

            v7 = new JSONArray();
            Iterator<Map.Entry<Thread, StackTraceElement[]>> v8 = v0_1.entrySet().iterator();
            do {
                if (!v8.hasNext()) {
                    v0_4 = v6.toString();
                    if (Logger.debug()) {
                        Logger.d("OOM_Exception", "size : " + v0_4.length() + " " + v0_4);
                        return v0_4;
                    } else {
                        return v0_4;
                    }
                }
                v0_2 = v8.next();
                if (v0_2 != null) {
                    v9 = new JSONObject();
                    v1 = ((Map.Entry) v0_2).getKey();
                    String v4 = ((Thread) v1).getName();
                    if (i.a.contains(v4)) {
                        v2 = 1;
                    } else {
                        Iterator v10 = i.a.iterator();
                        do {
                            if (v10.hasNext()) {
                                Object v2_1 = v10.next();
                                if (android.support.design.a.isTextEmpty(v4)) {
                                    continue;
                                }
                                if (!v4.startsWith(((String) v2_1))) {
                                    continue;
                                }
                                v2 = 1;
                                break;
                            } else {
                                v2 = 0;
                                break;
                            }
                        } while (true);
                    }
                    if (v2 != 0) {
                        continue;
                    }
                    v9.put("tr_n", ((Thread) v1).getName());
                    v0_2 = ((Map.Entry) v0_2).getValue();
                    if (v0_2 != null) {
                        v10_1 = new JSONArray();
                        int v11 = ((StackTraceElement[])v0_2).length;
                        v4_1 = 0;
                        while (true) {
                            int v0_3;
                            if (v4_1 < v11) {
                                StackTraceElement v12 = ((StackTraceElement[])v0_2)[v4_1];
                                String v13 = v12.getClassName();
                                if (i.b.contains(v13)) {
                                    v0_3 = 1;
                                } else {
                                    Iterator v14 = i.b.iterator();
                                    do {
                                        if (v14.hasNext()) {
                                            v1 = v14.next();
                                            if (android.support.design.a.isTextEmpty(v13)) {
                                                continue;
                                            }
                                            if (!v13.startsWith(((String) v1))) {
                                                continue;
                                            }
                                            v2 = 1;
                                        }
                                        break;
                                    } while (true);
                                    v10_1.put(v13 + "." + v12.getMethodName() + "(" + v12.getLineNumber() + ")");
                                    ++v4_1;
                                    continue;
                                }
                            } else {
                                v0_3 = v2;
                            }
                            if (v0_3 != 0) {
                                break;
                            }
                            v9.put("tr_st", v10_1);
                            if (v0_3 != 0) {
                                break;
                            }
                            v7.put(v9);
                            v6.put("tr_stacks", v7);
                            break;
                        }
                    } else {
                        int v0_3 = v2;
                        if (v0_3 != 0) {

                        }else {
                            v7.put(v9);
                            v6.put("tr_stacks", v7);
                        }
                        continue;
                    }
                } else {
                    v6.put("tr_stacks", v7);
                    continue;
                }
            } while (true);
        } catch (Throwable v0) {
            return "";
        }
    }

    private static String a(Context arg1) {
        String v0;
        if (arg1 == null) {
            v0 = "";
            return v0;
        }

        try {
            v0 = i.a(arg1.getFilesDir()).toString();
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
            v0 = "";
        }

        return v0;
    }

    private static JSONArray a(File arg7) {
        JSONArray v0 = new JSONArray();
        if (arg7 != null && (arg7.exists())) {
            if (arg7.isFile()) {
                v0.put(arg7.getName());
                return v0;
            }

            File[] v3 = arg7.listFiles();
            if (v3 != null) {
                int v4 = v3.length;
                int v2;
                for (v2 = 0; v2 < v4; ++v2) {
                    File v1 = v3[v2];
                    if (v1.isFile()) {
                        v0.put(v1.getName());
                        continue;
                    }

                    if (v1.isDirectory()) {
                        try {
                            JSONObject v5 = new JSONObject();
                            v5.put(v1.getName(), i.a(v1));
                            v0.put(v5);
                        } catch (JSONException v1_1) {
                            v1_1.printStackTrace();
                        }
                    }
                }
            }
        }

        return v0;
    }

    private static boolean a(Throwable arg3) {
        boolean v0 = false;
        if (arg3 != null) {
            int v1 = 0;
            while (arg3 != null) {
                if ((arg3 instanceof OutOfMemoryError)) {
                    return true;
                }

                if (v1 > 20) {
                    return v0;
                }

                ++v1;
                try {
                    arg3 = arg3.getCause();
                    continue;
                } catch (Throwable v1_1) {
                    return v0;
                }
            }
        }

        return v0;
    }

    private static boolean a(String arg3, Throwable arg4) {
        boolean v0 = false;
        if (arg3 != null && (arg3.endsWith(":ad"))) {
            int v1 = 0;
            while (arg4 != null) {
                if ((arg4 instanceof NullPointerException)) {
                    return true;
                }

                if (v1 > 20) {
                    return v0;
                }

                ++v1;
                try {
                    arg4 = arg4.getCause();
                    continue;
                } catch (Throwable v1_1) {
                    return v0;
                }
            }
        }

        return v0;
    }
}

